package com.ujs.eduservice.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ujs.eduservice.entity.EduCourse;
import com.ujs.eduservice.entity.EduTeacher;
import com.ujs.eduservice.mapper.EduTeacherMapper;
import com.ujs.eduservice.service.EduCourseService;
import com.ujs.eduservice.service.EduTeacherService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import static com.ujs.commonutils.utils.PackageListPage.packageListPage;

/**
 * <p>
 * 讲师 服务实现类
 * </p>
 *
 * @author testjava
 * @since 2023-07-28
 */
@Service
public class EduTeacherServiceImpl extends ServiceImpl<EduTeacherMapper, EduTeacher> implements EduTeacherService {

    @Autowired
    private EduCourseService courseService;

    @Override
    @Cacheable(value = "index", key = "'hotTeacher'")
    public List<EduTeacher> getHotTeacher() {
        QueryWrapper<EduTeacher> teacherWrapper = new QueryWrapper<EduTeacher>().orderByDesc("sort").last("limit 4");
        return baseMapper.selectList(teacherWrapper);
    }

    @Override
    public Map<String, Object> getTeacherList(Long page, Long limit) {
        Page<EduTeacher> teacherPage = new Page<>(page, limit);
        QueryWrapper<EduTeacher> queryWrapper = new QueryWrapper<EduTeacher>().orderByDesc("id");
        baseMapper.selectPage(teacherPage, queryWrapper);
        return packageListPage(teacherPage, queryWrapper);
    }

    @Override
    public Map<String, Object> getTeacherInfo(String teacherId) {
        // 1.根据id查询讲师信息
        EduTeacher eduTeacher = baseMapper.selectById(teacherId);
        // 2.根据讲师id查询讲师所讲课程
        QueryWrapper<EduCourse> courseWrapper = new QueryWrapper<EduCourse>().eq("teacher_id", teacherId).last("limit 4");
        List<EduCourse> courseList = courseService.list(courseWrapper);
        // 3.封装数据
        HashMap<String, Object> map = new HashMap<>();
        map.put("courseList", courseList);
        map.put("teacher", eduTeacher);
        return map;
    }
}
